define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    var raiseFinanceBannersModule = angular.module('raiseFinanceBannersModule', []);

    function raiseFinanceBannerService($http, $q, $location) {
        var service = {
            banners: [],//banners列表

            initData: function () {
                var promises = [
                    $http.get('/api/index.php?r=rclc/banner/get-banner-list')
                ];

                return $q.all(promises).then(function (xhrs) {
                    var i = 0;
                    service.banners = xhrs[i++].data.data.items;
                });
            },
            // 初始化查询表单数据
            initEditBannerDialog: function ($data, $dialog, $dialogScope, banner) {
                if (banner) {
                    angular.extend($data, {banner: angular.extend({}, banner)});
                    $data.banner.publish_status = parseInt(banner.publish_status);
                } else {
                    angular.extend($data, {banner: {publish_status: 1}});
                }

                $dialogScope.uploadImages = function (status) {
                    switch (status.status) {
                        case 'error':
                            if (status.msg.toString().match(/File\s+size\s+error/)) {
                                $dialogScope.uploader.state = '图片大小超出限制';
                            } else if (status.msg.toString().match(/File\s+extension\s+error/)) {
                                $dialogScope.uploader.state = '图片类型不符';
                            }
                            else {
                                $dialogScope.uploader.state = status.msg;
                            }

                            break;
                    }
                }

            },
            save: function ($data) {
                var data = {banner: angular.extend({}, $data.banner)};
                var url = api_prefix + "/rclc/banner/add-banner";
                if (data.banner.banner_id) {
                    url = api_prefix + "/rclc/banner/update-banner";
                }
                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
            },
            delete: function (banner) {
                util.confirm('确定要删除此Banner吗?', {
                    ok: function () {
                        var data = {bannerId: banner.banner_id};
                        var url = api_prefix + "/rclc/banner/delete-banner-by-id";
                        util.myAjax({
                            url: url,
                            type: "POST",
                            data: data,
                            context: this,
                            success: function (json) {
                                if (json.retCode == 0) {
                                    service.initData();
                                    ui.showSucTip();
                                } else {
                                    ui.showErrTip(json.errMsg);
                                }
                            }
                        });
                    }
                });
            },
            setStatus: function (banner, status) {
                var data = {bannerId: banner.banner_id, status: status};
                var url = api_prefix + "/rclc/banner/update-banner-status";
                util.myAjax({
                    url: url,
                    type: "POST",
                    data: data,
                    context: this,
                    success: function (json) {
                        if (json.retCode == 0) {
                            service.initData();
                            ui.showSucTip();
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
            }
        };

        return service;
    }

    raiseFinanceBannersModule.factory('raiseFinanceBanners', ['$http', '$q', '$location', raiseFinanceBannerService]);

    module.exports = raiseFinanceBannersModule;
});